package com.lycrsoft.springboothystrix.controller;

import com.lycrsoft.springboothystrix.utils.IdUtil;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 功能描述
 *
 * @author chatgpt_9527@126.com
 */

@RestController
@RequestMapping("/order")
@Slf4j
public class OrderHystrixController {
    @HystrixCommand(fallbackMethod = "circuitFallback", commandProperties = {
        @HystrixProperty(name = "circuitBreaker.enabled", value = "true"),//是否开启断路器
        @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "10"),// 请求次数
        @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "10000"),// 时间窗口期
        @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "60")// 失败率达到多少后跳
    })
    @GetMapping(value = "/hystrix/circuit/{id}")
    public String paymentCircuit(@PathVariable("id") Integer id) {
        if (id < 0) {
            throw new RuntimeException("id不能为负数");
        }
        String serial = IdUtil.simpleUUID();
        return "调用成功，线程池：" + Thread.currentThread().getName() + "访问paymentInfoTimeOut,serial=" + serial;
    }

    public String circuitFallback(Integer id) {
        return "测试断路器，线程池：" + Thread.currentThread().getName() + "访问paymentInfoTimeOut,id=" + id;

    }
}
